<?php

// invoke connection
require_once("conn.php");

function fetchData($sql) {
    $result = mysql_query($sql);

    if ($result != false) {
        $data = array();
        $i = 0;
        while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) != false) {
            $data [$i] = $row;
            $i++;
        }
        mysql_free_result($result);
        return $data;
    }
    return $result;
}

function getCount($sql) {
    $result = mysql_query($sql);
    if ($result != false) {
        $data = array();
        $i = 0;
        while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) != false) {
            $data [$i] = $row;
            $i++;
        }
        mysql_free_result($result);
        if ($data != false && count($data) > 0) {
            return $data[0]['count'];
        }
    }
    return 0;
}

/*
  global operations
 */

// update textsort by module
function update_textsort($module, $textid, $textsort) {
    $sql = "update " . $module . " set textsort ='" . $textsort . "' where id ='" . $textid . "'";
    mysql_query($sql);
}

//get posts by sort id
function getPostsBySort($sortid) {
    $sql = "select * from cdb_posts p,cdb_forums f where f.fup = " . $sortid . " and f.fid = p.fid and p.first = 1 order by dateline desc limit 4;";
    return mysql_query($sql);
}

//get members which has pictures
function getMemberWithPhoto() {
    $sql = "select * from uchome_space where avatar = 1 limit 0,6";
    return mysql_query($sql);
}

//get the activite members
function getActiveMember() {
    $sql = "select * from uchome_space order by updatetime desc limit 0,8";
    return mysql_query($sql);
}

//get hot topics
function getHotTopic() {
    $sql = "SELECT * FROM uchome_blog WHERE dateline>UNIX_TIMESTAMP()-86400*3 ORDER BY replynum DESC";
    return mysql_query($sql);
}

//get the recommend post
function getRecommendPost() {
    $sql = "SELECT distinct ub.uid as uid,ub.subject as subject,ubf.message as message,ub.blogid as blogid FROM uchome_blog ub,uchome_blogfield ubf where ub.blogid = ubf.blogid ORDER BY ub.dateline DESC,ub.replynum desc LIMIT 0,3;";
    return mysql_query($sql);
}

//get the user doing
function getDoing() {
    $sql = "select uid,username,message from uchome_doing where doid in (select max(doid) from uchome_doing group by uid order by dateline desc) order by doid desc limit 0,8;";
    return mysql_query($sql);
}

//get my travel
function getMyTravel($uid) {
    $sql = "select * from pcd_travel where postuser =" . $uid;
    return mysql_query($sql);
}

//get my food
function getMyFood($uid) {
    $sql = "select * from pcd_food where postuser = " . $uid;
    return mysql_query($sql);
}

///// hot/////////////////////////////////////////////////////	
///// hot/////////////////////////////////////////////////////	
///// hot/////////////////////////////////////////////////////	
function add_click_hot($module, $id) {
    if ($module == "travel") {
        $sql = "update pcd_travel set hot = hot + 1 where id = " . $id;
    } else if ($module == "route") {
        $sql = "update pcd_route set hot = hot + 1 where rid = " . $id;
    }
    mysql_query($sql);
}

///// delete/////////////////////////////////////////////////////	
///// delete/////////////////////////////////////////////////////	
///// delete/////////////////////////////////////////////////////	

function delete($module, $id) {
    $sql = "";
    if ($module == "basic") {
        $sql = "delete from pcd_websit where id='" . $id . "'";
    } else if ($module == "search") {
        $sql = "delete from pcd_search where id='" . $id . "'";
    } else if ($module == "sister_link") {
        $sql = "delete from pcd_sister_link where id='" . $id . "'";
    } else if ($module == "bbs_top") {
        $sql = "delete from pcd_bbs_top where id='" . $id . "'";
    } else if ($module == "suggest") {
        $sql = "delete from pcd_suggest where id='" . $id . "'";
    } else if ($module == "special") {
        $sql = "delete from pcd_special where id='" . $id . "'";
    } else if ($module == "travel") {
        $sql = "delete from pcd_selectedtravel where id='" . $id . "'";
    } else if ($module == "food") {
        $sql = "delete from pcd_selectedfood where id='" . $id . "'";
    } else if ($module == "bbs_hot") {
        $sql = "delete from pcd_bbs_hot where id='" . $id . "'";
    } else if ($module == "favorite") {
        $sql = "delete from pcd_favorite where id='" . $id . "'";
    } else if ($module == "diary") {
        $sql = "delete from pcd_diary where id='" . $id . "'";
    } else if ($module == "album") {
        $sql = "delete from pcd_pic where id='" . $id . "'";
    } else if ($module == "flash") {
        $sql = "delete from pcd_flashhot where id='" . $id . "'";
    } else if ($module == "other2") {
        
    }
    mysql_query($sql);
}

///// sister link/////////////////////////////////////////////////////	
///// sister link/////////////////////////////////////////////////////	
///// sister link/////////////////////////////////////////////////////	
//get sister link
function getSisterLink() {
    $sql = "select * from pcd_sister_link";
    return mysql_query($sql);
}

//update sister link
function updateSisterLink($linkname, $link) {
    $sql = "delete from pcd_sister_link";
    if (mysql_query($sql) == 1) {
        for ($i = 0; $i < count($linkname); $i++) {
            addSisterLink($linkname[$i], $link[$i]);
        }
    }
}

//insert sister link
function addSisterLink($linkname, $link) {
    //echo "<p>insert into pcd_sister_link values(null,'".$linkname."','".$link."',now())";
    $sql = "insert into pcd_sister_link values(null,'" . $linkname . "','" . $link . "',now())";
    //echo "<p>";		
    mysql_query($sql);
}

function getLastId() {
    $sql = "SELECT LAST_INSERT_ID() as id;";
    return mysql_query($sql);
}

///// travel/////////////////////////////////////////////////////	
///// travel/////////////////////////////////////////////////////	
///// travel/////////////////////////////////////////////////////	
//insert travel
// get my travel list
function getMyTravelList($postuser) {
    $sql = "select * from pcd_travel,cdb_members where postuser = " . $postuser . " and postuser = uid order by fid desc,id";
    //$sql = "select a.*,b.title,c.username from pcd_travel a, pcd_travel b, cdb_members c where a.postuser = ".$postuser." and a.postuser = c.uid  and a.fid = b.id order by a.fid desc,a.id";	
    //echo $sql;	
    return mysql_query($sql);
}

// get all travel
function getAllTravelList($area) {
    if ($area == -1) {
        $sql = "select * from pcd_travel,cdb_members where postuser = uid order by fid desc,id";
    } else {
        $sql = "select * from pcd_travel,cdb_members where postuser = uid and area = " . $area . " order by fid desc,id";
    }

    return mysql_query($sql);
}

// get all travel
function getTravelListByPage($page, $area) {
    if ($page == 0) {
        $page = 1;
    }
    $start = ($page - 1) * 15;
    if ($area == -1) {
        $sql = "select * from pcd_travel,cdb_members where postuser = uid and ((fid = id) or (fid =0)) order by id desc,fid desc,id limit " . $start . ", 15";
    } else {
        $sql = "select * from pcd_travel,cdb_members where postuser = uid and ((fid = id) or (fid =0)) and area = " . $area . " order by id desc,fid desc,id limit " . $start . ", 15";
    }
    return mysql_query($sql);
}

function getRouteListByPage($page, $area, $order) {
    $start = ($page - 1) * 15;
    if ($area == -1 || $area == '') {
        $sql = "select * from pcd_route where state = 1 order by hot " . $order . " limit " . $start . ", 15 ";
    } else {
        $sql = "select * from pcd_route where state = 1 and area = " . $area . " order by hot " . $order . " limit " . $start . ", 15 ";
    }
    return mysql_query($sql);
}

// get my travel list
function getMyTravelListByPage($postuser, $page) {
    $start = ($page - 1) * 15;
    $sql = "select * from pcd_travel,cdb_members where postuser = " . $postuser . " and postuser = uid order by fid desc,id limit " . $start . ", 15";
    return mysql_query($sql);
}

// get travel by id
function getTravelByManageId($id) {
    $sql = "select * from pcd_travel where id = '" . $id . "'";
    return mysql_query($sql);
}

function getScenceByManageId($id) {
    $sql = "select a.*,b.title as travel_title, b.area as area_info from pcd_travel a,pcd_travel b where a.id = '" . $id . "' and a.fid = b.id";
    return mysql_query($sql);
}

// delete travel
function delTravelById($id) {
    $sql = "select id from pcd_travel where fid = '" . $id . "';";
    $result = mysql_query($sql);
    $ids = array();
    while (($rs = mysql_fetch_object($result)) != false) {
        array_push($ids, $rs->id);
    }
    for ($i = 0; $i < count($ids); $i++) {
        $sql = "delete from pcd_travel where id = '" . $ids[$i] . "';";
        mysql_query($sql);
    }
    $sql = "delete from pcd_travel where id = '" . $id . "';";
    mysql_query($sql);
    $sql = "delete from pcd_travel where fid = '" . $id . "'";
    mysql_query($sql);
}

// tuihui
function backTravel($id, $info) {
    $sql = "update pcd_travel set reason='" . $info . "', state=2, timestamp=now() where id ='" . $id . "';";
    mysql_query($sql);
}

// tongguo
function approveTravel($id) {
    $sql = "update pcd_travel set state=1, timestamp=now() where id ='" . $id . "';";
    mysql_query($sql);
}

// get food list filter
function getTravelListFilter($sql) {
    return mysql_query($sql);
}

// add comment for travel
//$comment = array(addslashes($fid),addslashes($content),addslashes($uid));
function addComment($comment) {
    $sql = "insert into pcd_comment (fid,comment,postuser,state,postdate,timestamp,module) values('" . $comment[0] . "','" . $comment[1] . "','" . $comment[2] . "',0,now(),now(),'travel')";

    mysql_query($sql);
}

function getCommentTravel($travelid) {
    //$sql = "select * from pcd_comment where module = 'travel' and fid = '".$travelid."'";
    $sql = "select * from pcd_comment,cdb_members where module = 'travel' and  postuser = uid and fid = '" . $travelid . "'";
    return mysql_query($sql);
}

function addTravelPhoto($travel) {
//	array(addslashes($uploadfile),addslashes($discuz_uid),addslashes($type),addslashes($area),lastid);
// $travel = array(addslashes($uploadfile),addslashes($fid),addslashes($discuz_uid),addslashes($type),addslashes($area),$lastid);
    $sql = "insert into pcd_upload_travel (src,userid,type,area,postdate,fid,module,src_small) values('" . $travel[0] . "','" . $travel[2] . "','" . $travel[3] . "','" . $travel[4] . "',now(),'" . $travel[5] . "','travel','" . $travel[6] . "')";

    mysql_query($sql);
}

function getPhotoByTravel($travelid) {
    $sql = "select * from pcd_upload_travel where fid = '" . $travelid . "'";
    //	echo $sql;
    return mysql_query($sql);
}

function getAllPhotoTravel() {
    $sql = "select * from pcd_upload_travel";
    return mysql_query($sql);
}

function getTravelAmount() {
    $sql = "select count(*) as amount from pcd_upload_travel";

    $result = mysql_query($sql);
    $row = mysql_fetch_row($result);
    $amount = $row['amount'];
    return $amount;
}

function getTravelPhotoPage($fid, $page) {
    $start = ($page - 1) * 7;
    $sql = "select * from pcd_upload_travel where fid = " . $fid . " limit " . $start . ",7";
    //echo $sql;
    return mysql_query($sql);
}

///// food/////////////////////////////////////////////////////	
///// food/////////////////////////////////////////////////////	
///// food/////////////////////////////////////////////////////	
//insert food
function addMyFood($food) {
//$food = array(addslashes($title),addslashes($area),addslashes($taste),addslashes($condition),addslashes($service),addslashes($expense),addslashes($num),addslashes($costdate),addslashes($content),addslashes($discuz_uid),addslashes($telephone),addslashes($type),addslashes($address));
    $sql = "insert into pcd_food values(null,null,'" . $food[0] . "','','" . $food[1] . "','" . $food[2] . "','" . $food[3] . "','" . $food[4] . "','" . $food[12] . "','" . $food[10] . "','" . $food[11] . "',null,'" . $food[5] . "','" . $food[6] . "','" . $food[8] . "',now(),'" . $food[9] . "',null,'0',now());";
    mysql_query($sql);
}

//edit food
function editMyFood($food) {
    $sql = "update pcd_food set title='" . $food[0] . "',area='" . $food[1] . "',taste='" . $food[2] . "',environment='" . $food[3] . "',service='" . $food[4] . "',address='" . $food[12] . "',telephone='" . $food[10] . "',type='" . $food[11] . "',expense='" . $food[5] . "',sum='" . $food[6] . "',content='" . $food[8] . "',timestamp=now() where id='" . $food[13] . "'";
    //	echo $sql;
    mysql_query($sql);
}

// get my food list
function getMyFoodList($postuser) {
    $sql = "selectt distinct(*) from pcd_food,cdb_members where postuser = " . $postuser . " and postuser = uid";
    return mysql_query($sql);
}

// get all food list
function getAllFoodList() {
    $sql = "select * from pcd_food";
    return mysql_query($sql);
}

// get food by id
function getFoodByManageId($id) {
    $sql = "select * from pcd_food where id = '" . $id . "'";
    return mysql_query($sql);
}

// get food list filter
function getFoodListFilter($sql) {
    return mysql_query($sql);
}

// delete food
function delFoodById($id) {
    $sql = "delete from pcd_food where id = '" . $id . "'";
    //	echo $sql;
    mysql_query($sql);
}

// back reason for food
function backFood($id, $info) {
    $sql = "update pcd_food set reason='" . $info . "', state=2, timestamp=now() where id ='" . $id . "';";
    mysql_query($sql);
}

////// alumn //////////////////////////////////////
////// alumn //////////////////////////////////////
////// alumn //////////////////////////////////////
// get albums
function getAlbums() {
    $sql = "select * from pcd_album order by picsort";
    return mysql_query($sql);
}

// update album
function updateAlbums($sort, $name, $link, $piclink) {
    $sql = "delete from pcd_album";
    if (mysql_query($sql) == 1) {
        for ($i = 0; $i < count($name); $i++) {
            addAlbum($sort[$i], $name[$i], $link[$i], $piclink[$i]);
        }
    }
}

// add album
function addAlbum($sort, $name, $link, $piclink) {
    $sql = "insert into pcd_album values(null,'" . $name . "','" . $link . "','" . $piclink . "','" . $sort . "')";
    //echo "<p>";		
    mysql_query($sql);
}

////// diary //////////////////////////////////////
////// diary //////////////////////////////////////
////// diary //////////////////////////////////////	
// get diarys on
function getDiarysOn() {
    $sql = "select * from pcd_diary where turn = 1 order by textsort,updatetime desc";
    return mysql_query($sql);
}

// get diarys off
function getDiarysOff() {
    $sql = "select * from pcd_diary where turn = 0 order by updatetime desc";
    return mysql_query($sql);
}

// get diary by id
function getDiaryById($id) {
    $sql = "select * from pcd_diary where id = " . $id;
    return mysql_query($sql);
}

// add diary
function addDiary($diary) {
    //$diary = array($title,$content,$textlink,$turn,$discuz_uid);
    $sql = "insert into pcd_diary values(null,'" . $diary[0] . "','" . $diary[1] . "','" . $diary[2] . "',1,'" . $diary[3] . "','" . $diary[4] . "',now());";
    mysql_query($sql);
}

// update diary
function updateDiary($diary) {
    $diary = array($id, $title, $content, $textlink, $turn, $username);
    $sql = "update pcd_diary set title='" . $title . "', content='" . $content . "', textlink='" . $textlink . "', turn='" . $turn . "', $user='" . $username . "' where id = '" . $diary[0];
    mysql_query($sql);
}

// turn diary
function turnDiary($id, $turn) {
    $sql = "update pcd_diary set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

function getDiaryThree() {
    $sql = "select * from pcd_diary order by textsort,turn desc limit 0,3";
    return mysql_query($sql);
}

////// keyword //////////////////////////////////////
////// keyword //////////////////////////////////////
////// keyword //////////////////////////////////////	
// update keyword and discription
function keywordSetting($keyword, $description, $user) {
    $sql1 = "delete from pcd_website";
    if (mysql_query($sql1) == 1) {
        $sql2 = "insert into pcd_website values(null,'" . $keyword . "','" . $description . "','" . $user . "')";
        mysql_query($sql2);
    }
}

// get keyword
function getKeyword() {
    $sql = "select * from pcd_website";
    return mysql_query($sql);
}

////// search keyword //////////////////////////////////////
////// search keyword //////////////////////////////////////
////// search keyword //////////////////////////////////////	
// get all search keyword
function getSearchKeyword() {
    $sql = "select * from pcd_search";
    return mysql_query($sql);
}

// update search keyword
function updateSearchKeyword($search, $user) {
    $sql = "delete from pcd_search";
    if (mysql_query($sql) == 1) {
        for ($i = 0; $i < count($search); $i++) {
            addSearchKeyword($search[$i], $user);
        }
    }
}

// add search keyword
function addSearchKeyword($search, $user) {
    $sql = "insert into pcd_search values(null,'" . $search . "','" . $user . "')";
    mysql_query($sql);
}

////// bbs hot //////////////////////////////////////
////// bbs hot //////////////////////////////////////
////// bbs hot //////////////////////////////////////	
function getbbsOn($forumsort) {
    $sql = "select * from pcd_bbs_hot where turn = 1 and forumsort = '" . $forumsort . "';";
    return mysql_query($sql);
}

function getbbsOff($forumsort) {
    $sql = "select * from pcd_bbs_hot where turn = 0 and forumsort = '" . $forumsort . "';";
    return mysql_query($sql);
}

function getbbsById($id) {
    $sql = "select * from pcd_bbs_hot where id = '" . $id . "';";
    return mysql_query($sql);
}

function addbbsHot($bbs) {
    $sql = "insert into pcd_bbs_hot values(null,'" . $bbs[0] . "','" . $bbs[1] . "','" . $bbs[2] . "',1,'" . $bbs[3] . "','" . $bbs[4] . "','" . $bbs[5] . "',now())";
    mysql_query($sql);
}

function updatebbsHot($bbs) {
    $sql = "update pcd_bbs_hot set title = '" . $bbs[1] . "', textlink = '" . $bbs[3] . "', user = '" . $bbs[2] . "', turn = '" . $bbs[4] . "', updatetime = now() where id = " . $bbs[0];

    mysql_query($sql);
}

// turn flash hot 
function turnBbs($id, $turn) {
    $sql = "update pcd_bbs_hot set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

function getBbsFour($forumsort) {
    $sql = "select * from pcd_bbs_hot where forumsort = " . $forumsort . " order by turn desc,textsort limit 0,4;";
    return mysql_query($sql);
}

////// flash hot //////////////////////////////////////
////// flash hot //////////////////////////////////////
////// flash hot //////////////////////////////////////	
function getFlashHotOn() {
    $sql = "select * from pcd_flashhot where turn = 1;";
    return mysql_query($sql);
}

function getFlashHotOff() {
    $sql = "select * from pcd_flashhot where turn = 0;";
    return mysql_query($sql);
}

function getFlashHotById($id) {
    $sql = "select * from pcd_flashhot where id = '" . $id . "';";
    return mysql_query($sql);
}

function addFlashHot($flash) {
    $sql = "insert into pcd_flashhot values(null,'" . $flash[0] . "','" . $flash[1] . "','" . $flash[4] . "','" . $flash[2] . "',now(),'" . $flash[3] . "',1);";
    mysql_query($sql);
}

function updateFlashHot($flash) {
    $sql = "update pcd_flashhot set title='" . $flash[1] . "',piclink='" . $flash[2] . "',pic='" . $flash[3] . "',postuser='" . $flash[4] . "',updatetime=now(),turn='" . $flash[5] . "' where id = " . $flash[0];
    mysql_query($sql);
}

// turn flash hot 
function turnHot($id, $turn) {
    $sql = "update pcd_flashhot set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

function getFlashWrite() {
    $sql = "select pic as url,piclink as link from pcd_flashhot order by textsort;";
    return mysql_query($sql);
}

////// favorite hot //////////////////////////////////////
////// favorite hot //////////////////////////////////////
////// favorite hot //////////////////////////////////////	
function getFavoriteOn() {
    $sql = "select * from pcd_favorite where turn = 1;";
    return mysql_query($sql);
}

function getFavoriteOff() {
    $sql = "select * from pcd_favorite where turn = 0;";
    return mysql_query($sql);
}

function getFavoriteById($id) {
    $sql = "select * from pcd_favorite where id = '" . $id . "';";
    return mysql_query($sql);
}

function addFavorite($favorite) {
    $sql = "insert into pcd_favorite values(null,'" . $favorite[0] . "','" . $favorite[1] . "','" . $favorite[4] . "','" . $favorite[2] . "',now(),'" . $favorite[3] . "',1);";
    mysql_query($sql);
}

function updateFavorite($favorite) {
    $sql = "update pcd_favorite set title = '" . $favorite[1] . "',piclink='" . $favorite[2] . "',pic='" . $favorite[3] . "',postuser='" . $favorite[4] . "',updatetime=now(),turn='" . $favorite[5] . "' where id='" . $favorite[0] . "'";
    mysql_query($sql);
}

// turn flash hot 
function turnFavorite($id, $turn) {
    $sql = "update pcd_favorite set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

function getFavoriteSix() {
    $sql = "select * from pcd_favorite order by turn desc,textsort limit 0,6";
    return mysql_query($sql);
}

////// suggest hot //////////////////////////////////////
////// suggest hot //////////////////////////////////////
////// suggest hot //////////////////////////////////////	
function getSuggestOn() {
    $sql = "select * from pcd_suggest where turn = 1;";
    return mysql_query($sql);
}

function getSuggestOff() {
    $sql = "select * from pcd_suggest where turn = 0;";
    return mysql_query($sql);
}

function getSuggestById($id) {
    $sql = "select * from pcd_suggest where id = '" . $id . "';";
    return mysql_query($sql);
}

function addSuggest($suggest) {
    $sql = "insert into pcd_suggest values(null,'" . $suggest[0] . "','" . $suggest[1] . "','" . $suggest[4] . "','" . $suggest[2] . "',now(),'" . $suggest[3] . "',1);";
    mysql_query($sql);
}

function updateSuggest($suggest) {
    $sql = "update pcd_suggest set title = '" . $suggest[1] . "',piclink='" . $suggest[2] . "',pic='" . $suggest[3] . "',postuser='" . $suggest[4] . "',updatetime=now(),turn='" . $suggest[5] . "' where id='" . $suggest[0] . "'";
    mysql_query($sql);
}

// turn  
function turnSuggest($id, $turn) {
    $sql = "update pcd_suggest set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

// get six suggest
function getFlashSix() {
    $sql = "select * from pcd_suggest order by turn desc, textsort limit 6;";
    return mysql_query($sql);
}

////// subject hot //////////////////////////////////////
////// subject hot //////////////////////////////////////
////// subject hot //////////////////////////////////////	
function getSubjectOn() {
    $sql = "select * from pcd_subject where turn = 1;";
    return mysql_query($sql);
}

function getSubjectOff() {
    $sql = "select * from pcd_subject where turn = 0;";
    return mysql_query($sql);
}

function getSubjectById($id) {
    $sql = "select * from pcd_subject where id = '" . $id . "';";
    return mysql_query($sql);
}

function addSubject($subject) {
    $sql = "insert into pcd_subject values(null,'" . $subject[0] . "','" . $subject[1] . "','" . $subject[4] . "','" . $subject[2] . "',now(),'" . $subject[3] . "',1);";
    mysql_query($sql);
}

function updateSubject($subject) {
    $sql = "update pcd_subject set title = '" . $subject[1] . "',piclink='" . $subject[2] . "',pic='" . $subject[3] . "',postuser='" . $subject[4] . "',updatetime=now(),turn='" . $subject[5] . "' where id='" . $subject[0] . "'";
    mysql_query($sql);
}

// turn  
function turnSubject($id, $turn) {
    $sql = "update pcd_subject set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

////// bbs top //////////////////////////////////////
////// bbs top //////////////////////////////////////
////// bbs top //////////////////////////////////////
function getBbsTopOn() {
    $sql = "select * from pcd_bbs_top where turn = 1;";
    return mysql_query($sql);
}

function getBbsTopOff() {
    $sql = "select * from pcd_bbs_top where turn = 0;";
    return mysql_query($sql);
}

function getBbsTopById($id) {
    $sql = "select * from pcd_bbs_top where id = '" . $id . "';";
    return mysql_query($sql);
}

function addBbsTop($subject) {
    $sql = "insert into pcd_bbs_top values(null,'" . $subject[0] . "','" . $subject[1] . "','" . $subject[4] . "','" . $subject[2] . "',now(),'" . $subject[3] . "',1);";
    mysql_query($sql);
}

function updateBbsTop($subject) {
    $sql = "update pcd_bbs_top set title = '" . $subject[1] . "',piclink='" . $subject[2] . "',pic='" . $subject[3] . "',postuser='" . $subject[4] . "',updatetime=now(),turn='" . $subject[5] . "' where id='" . $subject[0] . "'";
//		echo $sql;	
    mysql_query($sql);
}

// turn  
function turnBbsTop($id, $turn) {
    $sql = "update pcd_bbs_top set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

function getBbsTopThree() {
    $sql = "select * from pcd_bbs_top order by textsort,turn desc limit 0,3";
    return mysql_query($sql);
}

////// pic hot //////////////////////////////////////
////// pic hot //////////////////////////////////////
////// pic hot //////////////////////////////////////	
function getPicOn() {
    $sql = "select * from pcd_pic where turn = 1;";
    return mysql_query($sql);
}

function getPicOff() {
    $sql = "select * from pcd_pic where turn = 0;";
    return mysql_query($sql);
}

function getPicById($id) {
    $sql = "select * from pcd_pic where id = '" . $id . "';";
    return mysql_query($sql);
}

function addPic($favorite) {
    $sql = "insert into pcd_pic values(null,'" . $favorite[0] . "','" . $favorite[1] . "','" . $favorite[4] . "','" . $favorite[2] . "',now(),'" . $favorite[3] . "',1);";
    mysql_query($sql);
}

function updatePic($favorite) {
    $sql = "update pcd_pic set title = '" . $favorite[1] . "',piclink='" . $favorite[2] . "',pic='" . $favorite[3] . "',postuser='" . $favorite[4] . "',updatetime=now(),turn='" . $favorite[5] . "' where id='" . $favorite[0] . "'";
    mysql_query($sql);
}

// turn flash hot 
function turnPic($id, $turn) {
    $sql = "update pcd_pic set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

function getPicFour() {
    $sql = "select * from pcd_pic order by turn desc,textsort limit 0,4";
    return mysql_query($sql);
}

////// food //////////////////////////////////////
////// food //////////////////////////////////////
////// food //////////////////////////////////////	
function getFoodOn() {
    $sql = "select * from pcd_selectedfood where turn = 1;";
    return mysql_query($sql);
}

function getFoodOff() {
    $sql = "select * from pcd_selectedfood where turn = 0;";
    return mysql_query($sql);
}

function getFoodById($id) {
    $sql = "select * from pcd_selectedfood where id = '" . $id . "';";
    return mysql_query($sql);
}

function addFood($flash) {
    $sql = "insert into pcd_selectedfood values(null,'" . $flash[0] . "','" . $flash[1] . "','" . $flash[4] . "','" . $flash[5] . "','" . $flash[2] . "',now(),'" . $flash[3] . "',1);";

    mysql_query($sql);
}

function updateFood($food) {
    $sql = "update pcd_selectedfood set title='" . $food[1] . "', content='" . $food[6] . "', pic ='" . $food[3] . "', piclink='" . $food[2] . "', turn='" . $food[5] . "', postuser='" . $food[4] . "' where id = '" . $food[0] . "'";
    mysql_query($sql);
}

// turn flash hot 
function turnFood($id, $turn) {
    $sql = "update pcd_selectedfood set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

function getFoodThree() {
    $sql = "select * from pcd_selectedfood order by textsort desc limit 3";
    return mysql_query($sql);
}

////// travel //////////////////////////////////////
////// travel //////////////////////////////////////
////// travel //////////////////////////////////////	
function getTravelOn() {
    $sql = "select * from pcd_selectedtravel where turn = 1;";
    return mysql_query($sql);
}

function getTravelOff() {
    $sql = "select * from pcd_selectedtravel where turn = 0;";
    return mysql_query($sql);
}

//	function getTravelById($id){
//		$sql = "select * from pcd_selectedtravel where id = '".$id."';";
//		return mysql_query($sql);
//	}

function addSelectedTravel($flash) {
    $sql = "insert into pcd_selectedtravel values(null,'" . $flash[0] . "','" . $flash[1] . "','" . $flash[4] . "','" . $flash[5] . "','" . $flash[2] . "',now(),'" . $flash[3] . "',1);";

    mysql_query($sql);
}

function updateTravel($travel) {
    $sql = "update pcd_selectedtravel set title='" . $travel[1] . "', content='" . $travel[6] . "', pic ='" . $travel[3] . "', piclink='" . $travel[2] . "', turn='" . $travel[5] . "', postuser='" . $travel[4] . "' where id = '" . $travel[0] . "'";

    mysql_query($sql);
}

// turn flash hot 
function turnTravel($id, $turn) {
    $sql = "update pcd_selectedtravel set turn = '" . $turn . "' where id = " . $id;
    mysql_query($sql);
}

// get travel;
function getTravelThree() {
    $sql = "select * from pcd_selectedtravel order by textsort desc limit 3;";
    return mysql_query($sql);
}

function is_pic($pic_edit) {
    if (strpos($pic_edit, "jpg") > 0 || strpos($pic_edit, "gif") > 0 || strpos($pic_edit, "jpeg") > 0) {
        return true;
    } else {
        return false;
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////		
//get scenes 
function getscenes() {
    $sql = "select * from pcd_scences";
    return mysql_query($sql);
}

//get scene by id
function get_scene_by_id($cid) {
    $sql = "select * from pcd_scences where cid = " . $cid;
    return mysql_query($sql);
}

//new scene
function scene_add($title, $content, $type, $area, $userid) {
    if ($title != "") {
        $sql = "insert into pcd_scences values(null,'" . $title . "','" . $content . "','" . $type . "','" . $area . "',now(),'" . $userid . "')";
        if (mysql_query($sql) == 1) {
            return 1;
        }
    }
    return 0;
}

//update scene
function scene_update($cid, $title, $content, $type, $area, $userid) {
    if ($cid != "") {
        //$sql = "insert into pcd_scences values(null,'".$title."','".$content."','".$type."','".$area."',now(),'".$userid."')";			
        $sql = "update pcd_scences set title='" . $title . "',content='" . $content . "',type='" . $type . "',area='" . $area . "', postdate=now(),postuser='" . $userid . "' where cid ='" . $cid . "';";
        if (mysql_query($sql) == 1) {
            return 1;
        }
    }
    return 0;
}

//get max scene id
function get_max_scene_id($userid) {
    if ($userid != "") {
        $sql = "select max(cid) as maxid from pcd_scences where postuser = " . $userid;
        return mysql_query($sql);
    }
}

//insert image info to database
function image_add($name, $url, $jid, $sort, $userid) {
    if ($name != "") {
        $sql = "insert into pcd_images values(null,'" . $name . "','" . $url . "','" . $jid . "','" . $sort . "',now(),'" . $userid . "')";
        if (mysql_query($sql) == 1) {
            return 1;
        }
    }
    return 0;
}

//delete scene by id
function delete_scene_by_id($cid) {
    if ($cid != "") {
        $sql = "delete from pcd_scences where cid = " . $cid;
        if (mysql_query($sql) == 1) {
            $sql2 = "delete from pcd_images where sort = c and jid = " . $cid;
            //$sql3 = "delete from pcd_sights where cid = ".$cid;
            mysql_query($sql2);
            //mysql_query($sql3);
            return 1;
        }
    }
}

//new sight
function sight_add($title, $content, $userid, $cid) {

    if ($title != "") {
        $sql = "insert into pcd_sights values(null,'" . $title . "','" . $content . "',now(),'" . $userid . "','" . $cid . "')";
        if (mysql_query($sql) == 1) {
            return 1;
        }
    }
    return 0;
}

//get max sight id
function get_max_sight_id($userid, $cid) {
    if ($userid != "") {
        $sql = "select max(sid) as maxid from pcd_sights where postuser = " . $userid . " and cid = " . $cid;
        return mysql_query($sql);
    }
}

//get sights by scene id 
function get_sights_by_cid($cid) {
    $sql = "select * from pcd_sights where cid = " . $cid;
    return mysql_query($sql);
}

//delete sight by id
function delete_sight_by_id($sid) {
    if ($sid != "") {
        $sql = "delete from pcd_sights where sid = " . $sid;
        if (mysql_query($sql) == 1) {
            $sql2 = "delete from pcd_images where sort = s and jid = " . $sid;
            //$sql3 = "delete from pcd_sights where cid = ".$cid;
            mysql_query($sql2);
            //mysql_query($sql3);
            return 1;
        }
    }
}

//get sight by id
function get_sight_by_id($sid) {
    $sql = "select * from pcd_sights where sid = " . $sid;
    return mysql_query($sql);
}

//update sight
function sight_update($sid, $title, $content, $userid, $cid) {
    if ($sid != "") {
        //$sql = "insert into pcd_scences values(null,'".$title."','".$content."','".$type."','".$area."',now(),'".$userid."')";			
        $sql = "update pcd_sights set title='" . $title . "',content='" . $content . "', postdate=now(),postuser='" . $userid . "', cid='" . $cid . "' where sid ='" . $sid . "';";
//			echo $sql." <<<<<<<<<<<<<<<<<<<<<<<<<<";		
        if (mysql_query($sql) == 1) {
            return 1;
        }
    }
    return 0;
}

//////////////////////////////////////////////////////////////////////////////////////
function contact_add($name, $mobile, $info, $userid) {
    $sql = "insert into cts_contact(name,mobile,info,createtime,userid) values('" . $name . "','" . $mobile . "','" . $info . "',now(),'" . $userid . "')";
    if ($name != "") {
        if (mysql_query($sql) == 1) {
            return 1;
        }
    }
    return 0;
}

//????? ?????id
function deletebyid($contactid) {
    $sql = "delete from cts_contact where contactid=" . $contactid;
    if (mysql_query($sql) == 1) {
        return 1;
    }
    return 0;
}

//????? ????id
function deleteallbyuserid($userid) {
    $sql = "delete from cts_contact where userid=" . $userid;
    if (mysql_query($sql) == 1) {
        return 1;
    }
    return 0;
}

//?????
function updatebyid($contactid, $name, $mobile, $info) {
    $sql = "update cts_contact set name='" . $name . "', mobile='" . $mobile . "', info='" . $info . "' where contactid=" . $contactid . ";";
    if (mysql_query($sql) == 1) {
        return 1;
    }
    return 0;
}

//????
function userbyid($userid) {
    $sql = "select * from cts_users where userid=" . $userid . ";";
    return mysql_query($sql);
}

//?????
function getcontactsbyid($userid) {
    $sql = "select * from cts_contact where userid=" . $_SESSION['userid'];
    return mysql_query($sql);
}

//?????
/*
  function getcontactscount($userid){
  $sql = "select count(*) from cts_contact where userid=".$_SESSION['userid'];
  return mysql_query($sql);
  }
 */
//??????
function updatebyuserid($userid, $username, $name, $email, $mobile) {
    $sql = "update cts_users set username='" . $username . "',name='" . $name . "',email='" . $email . "',mobile='" . $mobile . "' where userid=" . $userid . ";";
    if (mysql_query($sql) == 1) {
        return 1;
    }
    return 0;
}

//??????
function updatepasswordbyuserid($userid, $new) {
    $sql = "update cts_users set password='" . $new . "' where userid=" . $userid . ";";
    if (mysql_query($sql) == 1) {
        return 1;
    }
    return 0;
}

?>